/***********************************************
 *				MochtioHost.java
 *				------------
 *
 *	Author:  		bin04017
 *	Created:		17.01.2007
 *	Last modifed:	17.01.2007
 *	Description:	MochtioHost.java is for
 *
 ************************************************/
package mochito.tranferFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

import mochito.MochitoException;

/**
 * @author bin04017
 * 
 */
public class MochitoHost {

	/**
	 * Member Address
	 */
	private InetAddress inetAddress = null;
	/**
	 * Member Port
	 */
	private Integer port = new Integer(56565);
	/**
	 * Constructor
	 * Set inetAddress to localhost and standard port 56565
	 * @throws MochitoException
	 */
	public MochitoHost() throws MochitoException {
		init();
	}
	/**
	 * Constructor
	 * Set inetAddress to localhost
	 * @param port
	 * @throws MochitoException 
	 */
	public MochitoHost(Integer port) throws MochitoException{
		init();
		setPort(port);
	}
	/**
	 * Constructor
	 * standard port 56565
	 * @param inetAddress
	 * @throws MochitoException
	 */
	public MochitoHost(InetAddress inetAddress) throws MochitoException{
		init(inetAddress);
	}
	/**
	 * Constructor
	 * standard port 56565
	 * @param host
	 * @throws MochitoException
	 */
	public MochitoHost(String host) throws MochitoException{
			init(host);
	}
	/**
	 * Constructor
	 * standard port 56565
	 * @param host
	 * @throws MochitoException
	 */
	public MochitoHost(MochitoHost host) throws MochitoException{
		init(host);
	}
	/**
	 * Constructor
	 * @param inetAddress
	 * @param port
	 * @throws MochitoException
	 */
	public MochitoHost(InetAddress inetAddress, Integer port) throws MochitoException{
		init(inetAddress);
		setPort(port);
	}
	/**
	 * Constructor
	 * @param host
	 * @param port
	 * @throws MochitoException
	 */
	public MochitoHost(String host, Integer port) throws MochitoException{
			init(host);
			setPort(port);
	}
	/**
	 * Constructor
	 * @param host
	 * @param port
	 * @throws MochitoException
	 */
	public MochitoHost(MochitoHost host, Integer port) throws MochitoException{
		init(host);
		setPort(port);
	}
		
	/**
	 * INIT
	 * @param inetAddress
	 * @throws MochitoException
	 */
	private void init(InetAddress inetAddress) throws MochitoException{initInetAddress(inetAddress);}
	/**
	 * INIT
	 * @throws MochitoException
	 */
	private void init() throws MochitoException{ initInetAddress(null);}
	/**
	 * INIT
	 * @param host
	 * @throws MochitoException
	 */
	private void init(String host) throws MochitoException {
		try {
			initInetAddress(InetAddress.getByName(host));
		} catch (UnknownHostException e) {
			throw new MochitoException(e);
		}
	}
	/**
	 * INIT
	 * @param host
	 * @throws MochitoException
	 */
	private void init(MochitoHost host) throws MochitoException{ initInetAddress(host.getInetAddress());}
	/**
	 * Init InetAddress
	 * @param inetAddress
	 * @throws MochitoException
	 */
	private void initInetAddress(InetAddress inetAddress) throws MochitoException {
		try {
			if (inetAddress == null) {
				inetAddress = InetAddress.getLocalHost();
			}
		} catch (UnknownHostException e) {
			try {
				inetAddress = InetAddress.getByName("localhost");
			} catch (UnknownHostException e1) {
				throw new MochitoException(e1);
			}
		} finally {
			setInetAddress(inetAddress);
		}
	}
	
	/**
	 * @return the inetAddress
	 */
	public InetAddress getInetAddress() {
		return inetAddress;
	}

	/**
	 * @param port the port to set
	 * @throws MochitoException 
	 */
	protected void setPort(Integer port) throws MochitoException {
		if(port <= 1000 || port >= 65535){
			throw new MochitoException("Port out of range");
		} else{
			this.port = port;
		}
	}
	/**
	 * 
	 * @return the hostName
	 */
	public String getHostName(){
		return inetAddress.getHostName();
	}
	/**
	 * @return the port
	 */
	public Integer getPort() {
		return port;
	}

	/**
	 * @param inetAddress the inetAddress to set
	 */
	protected void setInetAddress(InetAddress inetAddress) {
		this.inetAddress = inetAddress;
	}
}
